package multithread.treadLocal;

import java.util.concurrent.TimeUnit;

/**
 * Created by xi on 2017/9/17.
 * 测试结果

 线程1永远输出abc

 线程2永远输出null

 看起来很美好.但是也有需要注意的地方

 */
public class Test1 {
    static final ThreadLocal<String> threadParam = new ThreadLocal<>();

    public static void main(String[] args) throws InterruptedException {
        //死循环,测多几次看结果
        while (true) {
            //线程1
            new Thread(() -> {
                //设置参数
                threadParam.set("abc");
                //输出参数

                System.out.println("t1,threadParam:"+threadParam);

                System.out.println("t1:" + threadParam.get());
                //看起来像是多余操作
              // threadParam.remove();

            }).start();
            TimeUnit.SECONDS.sleep(1);
            new Thread(() -> {
                //线程二,测试是否能获取abc,线程2 threadlocal拿不到数据

                System.out.println("t2,threadParam:"+threadParam);

                System.out.println("t2 data:" + threadParam.get());
            }).start();
        }
    }

}
